import Vue from 'vue'
import VueRouter from 'vue-router'
import { MessageBox } from 'element-ui';
import LayoutView from '../views/layout/LayoutView.vue'
import HomeView from '../views/home/HomeView.vue'
import AboutView from '../views/about/AboutView.vue'
import StudentsListView from '@/views/students/StudentsListView.vue'
import StudentsAddView from '@/views/students/StudentsAddView.vue'
import StudentsUnOpenView from '@/views/students/StudentsUnOpenView.vue'
import TeachersListView from '@/views/teachers/TeachersListView.vue'
import TeachersAddView from '@/views/teachers/TeachersAddView.vue'
import TeachersUnOpenView from '@/views/teachers/TeachersUnOpenView.vue'
import ClassListView from '@/views/class/ClassListView.vue'

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
return originalPush.call(this, location).catch((err) => err);
}
const originalReplace = VueRouter.prototype.replace;
VueRouter.prototype.replace = function replace(location) {
return originalReplace.call(this, location).catch((err) => err);
}

Vue.use(VueRouter)
// 11 材料学院  21a 计算机与软件学院  31核学员与经管学院 41 文学院
// 我本来要去 11号楼上课  计算机网络原理   课程临时取消   回办公室 主任在找你 21a   
const routes = [
  {
    path: "/",
    name: "layout",
    component: LayoutView,
    // 路由重定向 
    redirect: "/home",
    children: [
      {
        path: "home",
        name: "home",
        component: HomeView,
        meta:{
          title:"首页",
          breadcrumb:[
             {
                title: "首页",
                path: "/home",
             }
          ]
        }
      },
      {
        path: "students/list",
        name: "students/list",
        component: StudentsListView,
        meta:{
          title:"学生列表",
          breadcrumb:[
            {
              title: "首页",
              path: "/home",
           },
            {
               title: "学生管理",
               path: "",
            },
            {
              title: "学生列表",
              path: "/students/list",
           }
         ]
        }
      },
      {
        path: "students/unopenlist",
        name: "students/unopenlist",
        component: StudentsUnOpenView,
        meta:{
          title:"学生账号审核",
          breadcrumb:[
            {
              title: "首页",
              path: "/home",
           },
            {
               title: "学生管理",
               path: "",
            },
            {
              title: "学生账号审核",
              path: "/students/unopenlist",
           }
         ]
        }
      },
      {
        path: "students/add",
        name: "students/add",
        component: StudentsAddView,
        meta:{
          title:"学生新增",
          breadcrumb:[
            {
              title: "首页",
              path: "/home",
           },
            {
               title: "学生管理",
               path: "",
            },
            {
              title: "学生新增",
              path: "/students/add",
           }
         ]
        }
      },
      {
        path: "teachers/add",
        name: "teachers/add",
        component: TeachersAddView,
        meta:{
          title:"教师新增",
          breadcrumb:[
            {
              title: "首页",
              path: "/home",
           },
            {
               title: "教师管理",
               path: "",
            },
            {
              title: "教师新增",
              path: "/teachers/add",
           }
         ]
        }
      },
      {
        path: "teachers/list",
        name: "teachers/list",
        component: TeachersListView,
        meta:{
          title:"教师列表",
          breadcrumb:[
            {
              title: "首页",
              path: "/home",
           },
            {
               title: "教师管理",
               path: "",
            },
            {
              title: "教师列表",
              path: "/teachers/list",
           }
         ]
        }
      },
      {
        path: "teachers/unopenlist",
        name: "teachers/unopenlist",
        component: TeachersUnOpenView,
        meta:{
          title:"教师账号审核",
          breadcrumb:[
            {
              title: "首页",
              path: "/home",
           },
            {
               title: "教师管理",
               path: "",
            },
            {
              title: "教师教师账号审核",
              path: "/teachers/unopenlist",
           }
         ]
        }
      },

      {
        path: "class/classlist",
        name: "class/classlist",
        component: ClassListView,
        meta:{
          title:"班级列表",
          breadcrumb:[
            {
              title: "首页",
              path: "/home",
           },
            {
               title: "班级管理",
               path: "",
            },
            {
              title: "班级列表",
              path: "/class/classlist",
           }
         ]
        }
      },
      {
        path: "about",
        name: "about",
        component: AboutView,
        meta:{
          title:"关于我们"
        }
      }
    ]
  },
  {
    path: "/login",
    name: "login",
    component: () => import('@/views/login/LoginView.vue')
  },
  {
    path: "*",
    name: "error",
    component: () => import('../views/error/ErrorView.vue')
  }
]

const router = new VueRouter({
  routes,
  mode: 'history'
})

// 路由的守卫 是对路由访问的首位 当去到不去要鉴权的页面可以直接访问 需要鉴权的页面则需要再次判断他的token
router.beforeEach((to, from, next) => {
  if (to.path != '/login') {
    if (localStorage?.token) {
      next()
    } else {
      MessageBox.confirm('登录失效，请重新登录', '身份失效', {
        type: 'warning',
        confirmButtonText: '确定',
        showCancelButton: false,
      }).then(() => {
        next('/login')
      })
    }
  } else {
    next();
  }
})

router.afterEach((to, from) => {
  document.title = "DLW-" + to.meta.title
})

export default router
